package com.yu.yupicture.controller;


import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

import com.yu.yupicture.Untils.ThrowUntil;
import com.yu.yupicture.annotation.AuthCheck;
import com.yu.yupicture.common.BaseReponse;
import com.yu.yupicture.common.DeleteRequest;
import com.yu.yupicture.common.Result;
import com.yu.yupicture.constans.CacheConstans;
import com.yu.yupicture.constans.UserConstans;
import com.yu.yupicture.exception.ErrorCode;
import com.yu.yupicture.exception.biusnessException;
import com.yu.yupicture.manger.cache.muchCache;
import com.yu.yupicture.model.domain.dto.space.SpaceAddRequest;
import com.yu.yupicture.model.domain.dto.space.SpaceEditRequest;
import com.yu.yupicture.model.domain.dto.space.SpaceQueryRequest;
import com.yu.yupicture.model.domain.dto.space.SpaceUpdateRequest;
import com.yu.yupicture.model.domain.entry.Space;
import com.yu.yupicture.model.domain.entry.User;
import com.yu.yupicture.model.domain.vo.SpaceVO;
import com.yu.yupicture.service.PictureService;
import com.yu.yupicture.service.SpaceService;
import com.yu.yupicture.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@Api(tags = "空间相关接口")
@Slf4j
@RestController
@RequestMapping("/space")
public class SpaceController {

    @Resource
    private UserService userService;
    @Resource
    private SpaceService SpaceService;

    @Resource
    private muchCache muchCache;
    @Resource
    private PictureService pictureService;

    /**
     * 添加空间
     */

    @PostMapping("/add")
    @ApiOperation("添加空间")
    public BaseReponse<Long> addSpace(@RequestBody SpaceAddRequest SpaceAddRequest, HttpServletRequest request) throws IOException {
        User user = userService.getLoginUser(request);
        ThrowUntil.throwIf(user == null, ErrorCode.NOT_LOGIN_ERROR, "未登录");
        return Result.success(SpaceService.addSpace(SpaceAddRequest,user));
    }
    /**
     * 分页查询-管理端
     *
     * @throws IOException
     */
    @PostMapping("/list/page")
    @ApiOperation("分页查询-管理端")
    @AuthCheck(value = UserConstans.ADMIN)
    public BaseReponse<Page<Space>> page(@RequestBody SpaceQueryRequest SpaceQueryRequest) throws IOException {
        ThrowUntil.throwIf(SpaceQueryRequest == null, ErrorCode.PARAMS_ERROR, "请求参数为空");
        return Result.success(SpaceService.page(SpaceQueryRequest));
    }

    /**
     * 分页查询-用户端
     *
     * @throws IOException
     */
    @PostMapping("/list/page/vo")
    @ApiOperation("分页查询-用户端")
    public BaseReponse<Page<SpaceVO>> pageVo(@RequestBody SpaceQueryRequest SpaceQueryRequest, HttpServletRequest request) throws IOException {
        ThrowUntil.throwIf(SpaceQueryRequest == null, ErrorCode.PARAMS_ERROR, "请求参数为空");
        User user = userService.getLoginUser(request);
        ThrowUntil.throwIf(user == null, ErrorCode.NOT_LOGIN_ERROR, "未登录");
        //限制爬虫
        ThrowUntil.throwIf(SpaceQueryRequest.getPageSize() > 20, ErrorCode.PARAMS_ERROR, "请求参数过大");
        return Result.success(SpaceService.pageVo(SpaceQueryRequest, user));
    }

    @ApiOperation("根据id查询空间-管理员")
    @GetMapping("/get")
    @AuthCheck(value = UserConstans.ADMIN)
    public BaseReponse<Space> getSpaceById(@RequestParam("id") Long id) {
        return Result.success(SpaceService.getSpaceById(id));
    }

    @ApiOperation("根据id查询空间-用户端")
    @GetMapping("/get/vo")
    public BaseReponse<SpaceVO> getSpaceById(@RequestParam("id") Long id, HttpServletRequest request) {
        User user = userService.getLoginUser(request);
        ThrowUntil.throwIf(user == null, ErrorCode.NOT_LOGIN_ERROR);
        return Result.success(SpaceService.getSpaceVoById(id));
    }

    @ApiOperation("根据id删除空间-管理员")
    @PostMapping("/delete")
    public BaseReponse<Boolean> deleteSpaceById(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
        User user = userService.getLoginUser(request);
        ThrowUntil.throwIf(user == null, ErrorCode.NOT_LOGIN_ERROR);
        if (!pictureService.isAdmin(user) && !user.getId().equals(deleteRequest.getId())) {
            throw new biusnessException(ErrorCode.NO_AUTH_ERROR, "没有权限");
        }
        return Result.success(SpaceService.deleteById(deleteRequest.getId()));
    }

    @PostMapping("/edit")
    @ApiOperation("更新空间-用户端")
    public BaseReponse<Boolean> UserupdateSpace(@RequestBody SpaceEditRequest SpaceEditRequest, HttpServletRequest request) {
        User user = userService.getLoginUser(request);
        ThrowUntil.throwIf(user == null, ErrorCode.NOT_LOGIN_ERROR);
        return Result.success(SpaceService.userUpdateSpace(SpaceEditRequest, user));
    }

    @AuthCheck(value = UserConstans.ADMIN)
    @PostMapping("/update")
    @ApiOperation("更新空间-管理端")
    public BaseReponse<Boolean> updateSpace(@RequestBody SpaceUpdateRequest SpaceUpdateRequest) {
        return Result.success(SpaceService.updateSpace(SpaceUpdateRequest));
    }


}
